<template>
	<div class="page getMoneyQrCode">
		<div class="scroll-view">
			<customHeader middleTitle="收款二维码" @leftCallBack="handleBack" @rightCallBack="scan">
				<i class="zhk-sm"></i>
			</customHeader>
			<div class="page-body">
				<ul class="QrCode-list">
					<li v-for="(item, index) in items" :key="item.id" @click="linkTo(item.code)">
						<div class="wrap">
              
							<span v-if="index==0">
								{{item.name}}(默认)</span>
                <span v-if="index>0">
								{{item.name}}</span>
							<div>
								<i class="zhk-list_rarrow"></i>
							</div>
						</div>
					</li>
         
				</ul>
			</div>
		</div>
	</div>
</template>

<script>
import Vue from "vue";
import customHeader from "../../components/header/header";
import { Toast, MessageBox } from "mint-ui";
import jsBridge from "../../tools/jsBridge";
import localStorageClient from "../../js/localstorage.js";
export default {
  name: "getMoneyQrCode",
  data() {
    return {
      openState: 0,
      items: []
    };
  },
  methods: {
    handleBack: function() {
      //左上角返回，跳转到门店管理列表
      MessageBox.close(false);
      this.$router.push({
        path: "/storeManage",
        query: CommonUtil.concatSessionId({})
      });
    },
    //扫码绑定
    scan() {
      var _this = this;
      try {
        jsBridge.scanQRCode({
          type:"bar",
          title:"扫描绑定二维码",
          text:"请将取景器对准未绑定的二维码"
        }, function(response) {
          if (response && (response.success===true || response.success==='true')) {
            var code = response.resultObject;
            //判断二维码是否正确
            var decollator = "p.do?c=";
            if (code && code.indexOf(decollator) > 0) {
              code = code.substring(
                code.indexOf(decollator)+decollator.length
              );
              _this.scanCallback(code);
            } else {
              Toast("二维码错误，请重新扫码");
            }
          }
          else if(response && response.code && response.code=='-10'){
            //未扫码，返回，不处理
          }
					else if(response&& response.code && response.code=='11'){
						Toast(response.msg);
					}
          else {
            Toast("扫码失败");
          }
        });
      } catch (err) {
        Toast("扫码异常");
      }
    },
    scanCallback: function(data) {
      var _this = this;
      var shopId = _this.shopId;
      var queryString = "shopId=" + shopId + "&qrCode=" + data;
      this.ajax({
        url: "/newapp/code/bindQrCode.do?" + queryString,
        type: "get",
        controlLoading: true
      })
        .then(function(res) {
          var queryData = CommonUtil.concatSessionId({
            shopId: shopId,
            msg: msg,
            code: data
          });
          if (res && String(res.success)=='true') {
            _this.$router.push({
              path: "/storeManage/getMoneyQrCode/bindingSuccess",
              query: queryData
            });
          } else {
            var msg = "绑定二维码失败";
            if(res.msg){
              if(res.msg.indexOf('绑定过')>0){
                  msg = '此二维码已被使用，请更换二维码或联系客服';
              }
            }
            queryData.msg = msg;
            _this.$router.push({
              path: "/storeManage/getMoneyQrCode/bindingFail",
              query: queryData
            });
          }
        })
        .catch(function(err) {

        });
    },
    injectParamObject: function(paramObj) {
      paramObj = paramObj || {};
      sessionStorage.setItem("paramObj", JSON.stringify(paramObj));
    },
    takeOutParamObject: function() {
      var rt = sessionStorage.getItem("paramObj");
      if (!rt) {
        return {};
      }
      return JSON.parse(rt);
    },
    toOpenWeixin() {
      // var sessionId = APP.sessionId;
      // var shopId = this.shopId;
      // var smsCache = localStorageClient.load("sms", 60);
      // if (smsCache) {
      //   var cache = this.takeOutParamObject() || {};
      //   cache.shopId = shopId;
      //   cache.type = "1"; //新增

      //   this.injectParamObject(cache);

      //     "toOpenWeixin->smsCache=" +
      //       smsCache +
      //       ",href=weixin_open_step1_wangshang.html"
      //   );
      //   location.href =
      //     "../html/weixin_open_step1_wangshang.html?" + sessionId;
      // } else {
      //   var cache = this.takeOutParamObject() || {};
      //   cache.dispatchPage = "weixin_open_step1_wangshang.html";
      //   cache.dispatchData = { type: "1", shopId: shopId };

      //   this.injectParamObject(cache);
      //   this.$router.push({
      //     path: "/SMSVerification",
      //     query: CommonUtil.concatSessionId({})
      //   });
      // }
      const smsCache = localStorageClient.load("sms", 120); //data.merchantStatus == constants.store.WEIXIN_PAY_STATUS.none
      if (smsCache) {
        this.$router.push({
          path: "/storeManage/openShroffAccount",
          query: { shopId: this.shopId }
        });
      } else {
        this.$router.push({
          path: "/SMSVerification",
          query: { nextPath: "/storeManage/openShroffAccount", shopId: this.shopId }
        });
      }
    },
    //查看收款二维码
    linkTo(code) {
      var openState = this.openState;
      var shopId = this.shopId;
      if (openState && openState === 3) {
        //去查看二维码
        this.$router.push({
          path: "/storeManage/getMoneyQrCode/qrCodeDetail",
          query: { code: code, shopId: shopId }
        });
      } else {
        if (openState && openState == 2) {
          //禁用收款帐号
          MessageBox({
            title: "提示",
            message: "该门店暂未开通收款账户，开通后收款码才能正常收款。",
            confirmButtonText: "确定"
          }).then(action => {
            //去查看二维码
            this.$router.push({
              path: "/storeManage/getMoneyQrCode/qrCodeDetail",
              query: { code: code, shopId: shopId }
            });
          });
        } else {
          MessageBox({
            title: "提示",
            message: "该门店暂未开通收款账户，开通后收款码才能正常收款。",
            confirmButtonText: "知道了"
          }).then(action => {
              //去查看二维码
              this.$router.push({
                path: "/storeManage/getMoneyQrCode/qrCodeDetail",
                query: { code: code, shopId: shopId }
              });
          });
        }
      }
    },
    bindData: function(data) {
      //页面初始化时ajax获取到数据后，绑定数据库到对象
      var len = data.length;
      for (let i = 0; i < len; i++) {
        var item = data[i];
        this.items.push(item);
      }
    }
  },
  created: function() {
    var shopId = this.$route.query.shopId;
    var _this = this;
    _this.shopId = shopId;
    this.ajax({
      url: "/app/qrcode/getQrCodes.do?shopId=" + shopId,
      type: "get",
      controlLoading: false
    })
      .then(function(res) {
        if (res.success) {
          var data = res.resultObject;
          if (data != null && JSON.stringify(data) != "{}") {
            _this.bindData(data);
          } else {

          }
        }
        _this.loading = false;
      })
      .catch(function(err) {

      });
    //获取门店详情验证是否开通收款帐户
    this.ajax({
      url: "/app/shop/getShopDetail.do?shopId=" + shopId,
      type: "get"
    })
      .then(function(res) {
        if (res.success) {
          var data = res.resultObject;
          if (data != null && JSON.stringify(data) != "{}") {
            var openState = 0;
            if (
              data.wxPayStatus == null ||
              data.wxPayStatus == 0 ||
              data.wxPayStatus == 5 ||
              data.wxPayStatus == 6
            ) {
              ////开通收款账户
              openState = 1;
            } else if (data.wxPayStatus == 3 || data.wxPayStatus == 4) {
              //禁用收款账户管理
              openState = 2;
            } else {
              //跳转收款账户管理
              openState = 3;
            }
            _this.openState = openState;
          }
        }
      })
      .catch(function(err) {
       
      });
  },
  components: {
    customHeader
  }
};
</script>
<style lang='scss' scoped>
@import "getMoneyQrCode";
</style>
